package com.leetcode.fenzhi;

import java.util.Arrays;

/**
 * leetcode 973 最接近原点的k个点
 */
public class Solution973 {
    public int[][] kClosest(int[][] points, int k) {
        if (points == null || points.length == 0) {
            return null;
        }

        int[] distance = new int[points.length];
        for (int i = 0; i < points.length; i++) {
            distance[i] = (int)Math.pow(points[i][0], 2) + (int)Math.pow(points[i][1], 2);
        }

        for (int i = 0; i < k; i++) {
            for (int j = i + 1; j < points.length; j++) {
                if (distance[j] < distance[i]) {
                    swap1(points, i, j);
                    swap2(distance, i, j);
                }
            }
        }

        return Arrays.copyOfRange(points, 0, k);
    }

    private void swap1(int[][] result, int i, int j) {
        int[] tmp = result[i];
        result[i] = result[j];
        result[j] = tmp;
    }

    private void swap2(int[] result, int i, int j) {
        int tmp = result[i];
        result[i] = result[j];
        result[j] = tmp;
    }

    public static void main(String[] args) {
        Solution973 solution973 = new Solution973();
        int a[][] = {{1,3},{-2,2},{2,-2}};
        System.out.println(solution973.kClosest(a, 2));
    }
}
